package ren.xuedao.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import ren.xuedao.dto.ExposerDTO;
import ren.xuedao.dto.SeckillExecution;
import ren.xuedao.dto.SeckillResult;
import ren.xuedao.entity.Seckill;
import ren.xuedao.enums.SeckillStatEnum;
import ren.xuedao.exception.RepeatKillException;
import ren.xuedao.exception.SeckillCloseException;
import ren.xuedao.service.SeckillService;

import java.time.Clock;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/seckill")
public class SeckillController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SeckillService seckillService;

    @GetMapping("/list")
    public String list(Model model) {
        List<Seckill> seckillList = seckillService.getSeckillList();

        model.addAttribute("list",seckillList);
        return "list";
    }
    @GetMapping("/{seckillId}/detail")
    public String detail(@PathVariable Long seckillId, Model model) {

        if (Objects.isNull(seckillId)) {
            return "redirect:/seckill/list";
        }
        Seckill seckill = seckillService.getById(seckillId);

        if (Objects.isNull(seckill)) {
            return "forward:/seckill/list";
        }
        model.addAttribute("seckill",seckill);
        return "detail";
    }

    //Ajax接口
    @PostMapping("/{seckillId}/exposer")
    @ResponseBody
    public SeckillResult<ExposerDTO> exposer(Long seckillId) {
        SeckillResult<ExposerDTO> res;

        try {
            ExposerDTO exposerDTO = seckillService.exportSeckillUrl(seckillId);
            res = new SeckillResult<ExposerDTO>(true, exposerDTO);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            res = new SeckillResult<ExposerDTO>(false,e.getMessage());

        }
        return res;
    }

    @PostMapping("/{seckillId}/{md5}/execution")
    @ResponseBody
    public SeckillResult<SeckillExecution> execute(@PathVariable Long seckillId,@PathVariable String md5,@CookieValue(value = "killPhone",required = false) Long phone) {

        SeckillResult<SeckillExecution> res;
        if (Objects.isNull(phone)) {
            return new SeckillResult<SeckillExecution>(false,"未登录");
        }

        try {
            SeckillExecution seckillExecution = seckillService.executeSeckill(seckillId, phone, md5);

            return new SeckillResult<SeckillExecution>(true,seckillExecution);

        } catch (SeckillCloseException e1) {
            SeckillExecution execution = new SeckillExecution(seckillId, SeckillStatEnum.END);
            return new SeckillResult<SeckillExecution>(true,execution);
        } catch (RepeatKillException e2) {
            SeckillExecution execution = new SeckillExecution(seckillId,SeckillStatEnum.REPEAT_KILL);
            return new SeckillResult<SeckillExecution>(true,execution);
        }catch (Exception e) {
            logger.error(e.getMessage(),e);

            return new SeckillResult<SeckillExecution>(true,new SeckillExecution(seckillId,SeckillStatEnum.INNER_ERROR));
        }
    }

    @GetMapping("/time/now")
    @ResponseBody
    public SeckillResult<Long> time() {
        long millis = Clock.systemUTC().millis();
        return new SeckillResult<Long>(true,millis);

    }
}
